package com.ifacebox.client.tools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author znn
 */
public class FaceLuckDrawSetupDialog extends JDialog implements ActionListener {
    private static final long serialVersionUID = 1L;
    private final FaceLuckDrawConfig faceLuckDrawConfig;
    private final JPanel fieldPanel, toolsPanel;
    private JComboBox<String> screenId, faceImageLuckDrawSwitch, faceLuckWinRemove;
    private JSpinner faceImageCols, faceImageRows, faceImageGap, faceImageSwitchMs, faceLuckDrawCountdown;
    private JTextArea faceLuckDrawTips;
    private JTextField faceImageFilePath, facePersonFilePath, faceLuckDrawListFile;
    private JButton save, cancel;

    public FaceLuckDrawSetupDialog(Frame frame, FaceLuckDrawConfig faceLuckDrawConfig) {
        super(frame);
        this.faceLuckDrawConfig = faceLuckDrawConfig;
        super.setTitle(frame.getTitle());
        super.setIconImage(frame.getIconImage());
        super.add(fieldPanel = new JPanel(), BorderLayout.CENTER);
        super.add(toolsPanel = new JPanel(), BorderLayout.SOUTH);
        this.initComponent();
        super.pack();
        super.setResizable(false);
        super.setLocationRelativeTo(frame);
    }

    public void initComponent() {
        Box fieldBox;
        fieldPanel.add(fieldBox = Box.createVerticalBox());
        fieldBox.add(addFieldBox("    屏幕编号：", screenId = new JComboBox<>(new String[]{"0", "1"})));
        fieldBox.add(addFieldBox("人脸图像列数：", faceImageCols = new JSpinner(new SpinnerNumberModel(16, 2, 1024, 1))));
        fieldBox.add(addFieldBox("人脸图像行数：", faceImageRows = new JSpinner(new SpinnerNumberModel(16, 2, 1024, 1))));
        fieldBox.add(addFieldBox("人脸图像间隔：", faceImageGap = new JSpinner(new SpinnerNumberModel(1, 0, 10, 1))));
        fieldBox.add(addFieldBox("人脸切换时间：", faceImageSwitchMs = new JSpinner(new SpinnerNumberModel(100, 1, 1000, 1))));
        fieldBox.add(addFieldBox("  中奖倒计时：", faceLuckDrawCountdown = new JSpinner(new SpinnerNumberModel(3, 0, 10, 1))));
        fieldBox.add(addFieldBox("中奖人脸切换：", faceImageLuckDrawSwitch = new JComboBox<>(new String[]{"true", "false"})));
        fieldBox.add(addFieldBox("中奖人脸删除：", faceLuckWinRemove = new JComboBox<>(new String[]{"true", "false"})));
        fieldBox.add(addFieldBox("中奖提示信息：", new JScrollPane(faceLuckDrawTips = new JTextArea(5, 30))));
        fieldBox.add(addFieldBox("人脸文件路径：", faceImageFilePath = new JTextField()));
        fieldBox.add(addFieldBox("信息文件路径：", facePersonFilePath = new JTextField()));
        fieldBox.add(addFieldBox("中奖名单文件：", faceLuckDrawListFile = new JTextField()));
        faceLuckDrawTips.setToolTipText("<html>【中奖提示信息变量】<br>身份证号码:{idNumber}<br>手机号码:{phoneNumber}<br>姓名:{idName}<br>公司:{companyName}<br>职务:{businessName}</html>");
        faceLuckDrawTips.setLineWrap(true);
        toolsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        toolsPanel.add(save = new JButton("保存(S)"));
        save.setMnemonic('S');
        save.addActionListener(this);
        toolsPanel.add(cancel = new JButton("取消(N)"));
        cancel.setMnemonic('N');
        cancel.addActionListener(this);
    }

    private static Box addFieldBox(String label, JComponent field) {
        Box box = Box.createHorizontalBox();
        box.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
        box.add(new JLabel(label));
        box.add(field);
        return box;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == save) {
            faceLuckDrawConfig.setScreenId(Integer.parseInt(screenId.getSelectedItem() == null ? "" : screenId.getSelectedItem().toString()));
            faceLuckDrawConfig.setFaceImageCols(Integer.parseInt(faceImageCols.getValue().toString()));
            faceLuckDrawConfig.setFaceImageRows(Integer.parseInt(faceImageRows.getValue().toString()));
            faceLuckDrawConfig.setFaceImageGap(Integer.parseInt(faceImageGap.getValue().toString()));
            faceLuckDrawConfig.setFaceImageSwitchMs(Integer.parseInt(faceImageSwitchMs.getValue().toString()));
            faceLuckDrawConfig.setFaceLuckDrawCountdown(Integer.parseInt(faceLuckDrawCountdown.getValue().toString()));
            faceLuckDrawConfig.setFaceImageLuckDrawSwitch(Boolean.parseBoolean(faceImageLuckDrawSwitch.getSelectedItem() == null ? "" : faceImageLuckDrawSwitch.getSelectedItem().toString()));
            faceLuckDrawConfig.setFaceLuckWinRemove(Boolean.parseBoolean(faceLuckWinRemove.getSelectedItem() == null ? "" : faceLuckWinRemove.getSelectedItem().toString()));
            faceLuckDrawConfig.setFaceLuckDrawTips(faceLuckDrawTips.getText());
            faceLuckDrawConfig.setFaceImageFilePath(faceImageFilePath.getText());
            if (!new File(FaceLuckDrawConfig.FILE_PATH, faceLuckDrawConfig.getFaceImageFilePath()).exists()) {
                JOptionPane.showMessageDialog(this, "人脸文件路径不存在！", this.getTitle(), JOptionPane.ERROR_MESSAGE);
                faceImageFilePath.requestFocus();
                return;
            }
            faceLuckDrawConfig.setFacePersonFilePath(facePersonFilePath.getText());
            if (!new File(FaceLuckDrawConfig.FILE_PATH, faceLuckDrawConfig.getFacePersonFilePath()).exists()) {
                JOptionPane.showMessageDialog(this, "信息文件路径不存在！", this.getTitle(), JOptionPane.ERROR_MESSAGE);
                facePersonFilePath.requestFocus();
                return;
            }
            faceLuckDrawConfig.setFaceLuckDrawListFile(faceLuckDrawListFile.getText());
            faceLuckDrawConfig.save();
            JOptionPane.showMessageDialog(this, "保存设置成功，重启客户端后生效。", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
            setVisible(false);
        }
        if (e.getSource() == cancel) {
            setVisible(false);
        }
    }

    @Override
    public void setVisible(boolean b) {
        if (b) {
            screenId.setSelectedItem(String.valueOf(faceLuckDrawConfig.getScreenId()));
            faceImageCols.setValue(faceLuckDrawConfig.getFaceImageCols());
            faceImageRows.setValue(faceLuckDrawConfig.getFaceImageRows());
            faceImageGap.setValue(faceLuckDrawConfig.getFaceImageGap());
            faceImageSwitchMs.setValue(faceLuckDrawConfig.getFaceImageSwitchMs());
            faceLuckDrawCountdown.setValue(faceLuckDrawConfig.getFaceLuckDrawCountdown());
            faceImageLuckDrawSwitch.setSelectedItem(String.valueOf(faceLuckDrawConfig.isFaceImageLuckDrawSwitch()));
            faceLuckWinRemove.setSelectedItem(String.valueOf(faceLuckDrawConfig.isFaceLuckWinRemove()));
            faceLuckDrawTips.setText(faceLuckDrawConfig.getFaceLuckDrawTips());
            faceImageFilePath.setText(faceLuckDrawConfig.getFaceImageFilePath());
            facePersonFilePath.setText(faceLuckDrawConfig.getFacePersonFilePath());
            faceLuckDrawListFile.setText(faceLuckDrawConfig.getFaceLuckDrawListFile());
        }
        super.pack();
        super.setVisible(b);
    }
}
